package com.jstyle.jclife.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.TextView;

import com.jstyle.blesdk.Util.ResolveUtil;
import com.jstyle.blesdk.constant.DeviceConst;
import com.jstyle.jclife.R;
import com.jstyle.jclife.ble.BleService;
import com.jstyle.jclife.fragment.EcgFragment;
import com.jstyle.jclife.model.BleData;
import com.jstyle.jclife.utils.ChartDataUtil;
import com.jstyle.jclife.utils.RxBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import lecho.lib.hellocharts.view.LineChartView;

public class EcgLandscapeActivity extends AppCompatActivity {

    @BindView(R.id.textView_ecg_hr)
    TextView textViewEcgHr;
    @BindView(R.id.textView_ecg_hrv)
    TextView textViewEcgHrv;
    @BindView(R.id.button3)
    ImageView button3;
    @BindView(R.id.button_hrv_level)
    ImageView buttonHrvLevel;

    @BindView(R.id.iv_change_display)
    ImageView ivChangeDisplay;
    @BindView(R.id.lineChartView_ppg)
    LineChartView lineChartViewPpg;
    @BindView(R.id.tv_ppg)
    TextView tvPpg;
    @BindView(R.id.lineChartView_ecg)
    LineChartView lineChartViewEcg;
    @BindView(R.id.tv_ecg)
    TextView tvEcg;
    int maxSize = 3072;
    int maxPpgSize = 1200;
    private Disposable subscription;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ecg_landscape);
        ButterKnife.bind(this);
        init();
    }

    private int index = 0;
    private int indexPpg=0;
    private void init() {
        initLineChartView();

        subscription = RxBus.getInstance().toObservable(BleData.class).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<BleData>() {
            @Override
            public void accept(BleData bleData) throws Exception {
                String action = bleData.getAction();
                if (action.equals(EcgFragment.ecgValueAction)) {
                    String value = bleData.getData();
                    queueEcg.add(Double.valueOf(value));
                    int size=queueEcg.size();
                    if(size>=maxSize){
                        index++;
                    }
                    if(size%79==0){
                        setLineChartViewEcgValue();
                    }

                }else if(action.equals(EcgFragment.ppgValueAction)){
                    String value = bleData.getData();
                    if (queuePpg.size() > maxPpgSize) queuePpg.remove(0);
                    queuePpg.add(Float.valueOf(value));
                    //if(queuePpg.size()%20==0){
                    indexPpg++;
                    if(indexPpg%30==0)
                        setLineChartViewPpgValue();
                   // }

                } else if (action.equals(EcgFragment.hrValueAction)) {
                    String value = bleData.getData();
                    textViewEcgHr.setText(value);
                } else if (action.equals(EcgFragment.hrvValueAction)) {
                    String value = bleData.getData();
                    textViewEcgHrv.setText(value);
                } else if (action.equals(BleService.ACTION_DATA_AVAILABLE)) {
                    byte[] value = bleData.getValue();
                    resolveData(value);
                }
            }
        });
    }
    private void resolveData(byte[] value) {

        switch (value[0]) {

            case DeviceConst.CMD_PPGGDATA:
//                for (int i = 0; i < value.length / 2 - 1; i++) {
//                   float ppgValue =ChartDataUtil.getPPgData(ResolveUtil.getValue(value[i * 2 + 1], 1) + ResolveUtil.getValue(value[i * 2 + 2], 0));
//                    if (ppgValue >= 32768) ppgValue = ppgValue - 65536;
//                    if (queuePpg.size() > maxPpgSize) queuePpg.remove(0);
//                    queuePpg.add(ppgValue);
//                }
//                setLineChartViewPpgValue();
                break;
            case DeviceConst.CMD_Result_ECGPPG:
                if(value[1] != (byte) 0xff && value[1] != 0x01 && value[1] != (byte) 0x0b){
                    if(value[1]==3){
                        finish();
                    }
                    queuePpg.clear();
                    queueEcg.clear();

                    setLineChartViewPpgValue();
                    setLineChartViewEcgValue();
                    textViewEcgHrv.setText("--");
                    textViewEcgHr.setText("--");
                    return;
                }

                break;
        }
    }

    private void setLineChartViewEcgValue() {

        int tempCount=index;
        lineChartViewEcg.setLineChartData(ChartDataUtil.getEcgLineChartData(this, queueEcg, Color.RED,7,tempCount));
    }

    private void setLineChartViewPpgValue() {
        final List<Float>drawList=queuePpg;
        lineChartViewPpg.setLineChartData(ChartDataUtil.getPpgLineChartData(this, drawList, Color.RED));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(subscription);
    }
    protected void unSubscribe(Disposable subscription) {
        if (subscription != null && !subscription.isDisposed()) {
            subscription.dispose();
        }
    }
    private void initLineChartView() {
        ChartDataUtil.initDataChartView(lineChartViewEcg, 0, 8000, maxSize, -8000);
    }

    private void initPPgView() {
        //  ChartDataUtil.initDataChartView(lineChartViewPpg, 0, 10000, maxSize, -10000);
        setLineChartViewPpgValue();
    }

    List<Double> queueEcg = new ArrayList<>();
    List<Float> queuePpg = new ArrayList<>();

    @OnClick(R.id.iv_change_display)
    public void onViewClicked() {
        finish();
    }
}
